package cn.kevinkun.WheelView;
/*
 * origin from https://gitee.com/mirrors/WheelView
 */

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class WheelView extends ScrollView {
	public static final String TAG = WheelView.class.getSimpleName();
	private Context context;
	private LinearLayout views;
	List<String> items;
	public static final int OFF_SET_DEFAULT = 1;
	int offset = 1;
	int displayItemCount;
	int selectedIndex = 1;
	int initialY;
	Runnable scrollerTask;
	int newCheck = 50;
	int itemHeight = 0;
	int[] selectedAreaBorder;
	private int scrollDirection = -1;
	private static final int SCROLL_DIRECTION_UP = 0;
	private static final int SCROLL_DIRECTION_DOWN = 1;
	Paint paint;
	int viewWidth;
	private OnWheelViewListener onWheelViewListener;
	int textColor;
	int textColorSelect;
	int textSize;
	int textSizeSelect;
	int indicatorColor;

	public WheelView(Context context) {
		super(context);
		init(context);
	}

	public WheelView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public WheelView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	private List<String> getItems() {
		return this.items;
	}

	public void setItems(List<String> list) {
		if (this.items == null) {
			this.items = new ArrayList();
		}
		this.items.clear();
		this.items.addAll(list);
		for (int i = 0; i < this.offset; i++) {
			this.items.add(0, "");
			this.items.add("");
		}
		initData();
	}

	public int getOffset() {
		return this.offset;
	}

	public void setOffset(int offset) {
		this.offset = offset;
	}

	public void setTextColor(int color) {
		this.textColor = color;
	}

	public void setTextColorSelect(int color) {
		this.textColorSelect = color;
	}

	public void setIndicatorColor(int color) {
		this.indicatorColor = color;
	}

	public void setTextSize(int size) {
		this.textSize = size;
	}

	public void setTextSizeSelect(int size) {
		this.textSizeSelect = size;
	}

	public void setItemHeight(int height) {
		this.itemHeight = height;
	}

	private void init(Context context) {
		this.context = context;

		Log.d(TAG, "parent: " + getParent());

		setVerticalScrollBarEnabled(false);

		this.views = new LinearLayout(context);
		this.views.setOrientation(1);
		addView(this.views);

		this.scrollerTask = new Runnable() {
			public void run() {
				int newY = WheelView.this.getScrollY();
				if (WheelView.this.initialY - newY == 0) {
					final int remainder = WheelView.this.initialY % WheelView.this.itemHeight;
					final int divided = WheelView.this.initialY / WheelView.this.itemHeight;
					if (remainder == 0) {
						WheelView.this.selectedIndex = (divided + WheelView.this.offset);

						WheelView.this.onSeletedCallBack();
					} else if (remainder > WheelView.this.itemHeight / 2) {
						WheelView.this.post(new Runnable() {
							public void run() {
								WheelView.this.smoothScrollTo(0,
										WheelView.this.initialY - remainder + WheelView.this.itemHeight);
								WheelView.this.selectedIndex = (divided + WheelView.this.offset + 1);
								WheelView.this.onSeletedCallBack();
							}
						});
					} else {
						WheelView.this.post(new Runnable() {
							public void run() {
								WheelView.this.smoothScrollTo(0, WheelView.this.initialY - remainder);
								WheelView.this.selectedIndex = (divided + WheelView.this.offset);
								WheelView.this.onSeletedCallBack();
							}
						});
					}
				} else {
					WheelView.this.initialY = WheelView.this.getScrollY();
					WheelView.this.postDelayed(WheelView.this.scrollerTask, WheelView.this.newCheck);
				}
			}
		};
	}

	public void startScrollerTask() {
		this.initialY = getScrollY();
		postDelayed(this.scrollerTask, this.newCheck);
	}

	private void initData() {
		this.displayItemCount = (this.offset * 2 + 1);
		for (String item : this.items) {
			this.views.addView(createView(item));
		}
		refreshItemView(0);
	}

	private TextView createView(String item) {
		TextView tv = new TextView(this.context);
		tv.setLayoutParams(new FrameLayout.LayoutParams(-1, -2));
		tv.setSingleLine(true);
		tv.setTextSize(this.textSize);
		tv.setText(item);
		tv.setGravity(17);
		tv.setTextColor(this.textColor);
		int padding = dip2px(15.0F);
		tv.setPadding(padding, padding, padding, padding);
		if (this.itemHeight == 0) {
			this.itemHeight = getViewMeasuredHeight(tv);
			Log.d(TAG, "itemHeight: " + this.itemHeight);
			this.views.setLayoutParams(new FrameLayout.LayoutParams(-1, this.itemHeight * this.displayItemCount));
			LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams();
			setLayoutParams(new LinearLayout.LayoutParams(lp.width, this.itemHeight * this.displayItemCount));
		}
		return tv;
	}

	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);

		refreshItemView(t);
		if (t > oldt) {
			this.scrollDirection = 1;
		} else {
			this.scrollDirection = 0;
		}
	}

	private void refreshItemView(int y) {
		int position = y / this.itemHeight + this.offset;
		int remainder = y % this.itemHeight;
		int divided = y / this.itemHeight;
		if (remainder == 0) {
			position = divided + this.offset;
		} else if (remainder > this.itemHeight / 2) {
			position = divided + this.offset + 1;
		}
		int childSize = this.views.getChildCount();
		for (int i = 0; i < childSize; i++) {
			TextView itemView = (TextView) this.views.getChildAt(i);
			if (itemView == null) {
				return;
			}
			if (position == i) {
				itemView.setTextColor(this.textColorSelect);
				itemView.setTextSize(this.textSizeSelect);
			} else {
				itemView.setTextColor(this.textColor);
				itemView.setTextSize(this.textSize);
			}
		}
	}

	private int[] obtainSelectedAreaBorder() {
		if (this.selectedAreaBorder == null) {
			this.selectedAreaBorder = new int[2];
			this.selectedAreaBorder[0] = (this.itemHeight * this.offset);
			this.selectedAreaBorder[1] = (this.itemHeight * (this.offset + 1));
		}
		return this.selectedAreaBorder;
	}

	public void setBackgroundDrawable(Drawable background) {
		if (this.viewWidth == 0) {
			this.viewWidth = ((Activity) this.context).getWindowManager().getDefaultDisplay().getWidth();
			Log.d(TAG, "viewWidth: " + this.viewWidth);
		}
		if (this.paint == null) {
			this.paint = new Paint();
			this.paint.setColor(this.indicatorColor);
			this.paint.setStrokeWidth(dip2px(1.0F));
		}
		background = new Drawable() {
			public void draw(Canvas canvas) {
				canvas.drawLine(0.0F, WheelView.this.obtainSelectedAreaBorder()[0], WheelView.this.viewWidth * 6 / 6,
						WheelView.this.obtainSelectedAreaBorder()[0], WheelView.this.paint);
				canvas.drawLine(0.0F, WheelView.this.obtainSelectedAreaBorder()[1], WheelView.this.viewWidth * 6 / 6,
						WheelView.this.obtainSelectedAreaBorder()[1], WheelView.this.paint);
			}

			public void setAlpha(int alpha) {
			}

			public void setColorFilter(ColorFilter cf) {
			}

			public int getOpacity() {
				return 0;
			}
		};
		super.setBackgroundDrawable(background);
	}

	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		Log.d(TAG, "w: " + w + ", h: " + h + ", oldw: " + oldw + ", oldh: " + oldh);
		this.viewWidth = w;
		setBackgroundDrawable(null);
	}

	private void onSeletedCallBack() {
		if (this.onWheelViewListener != null) {
			this.onWheelViewListener.onSelected(this.selectedIndex, (String) this.items.get(this.selectedIndex));
		}
	}

	public void setSeletion(int position) {
		final int p = position;
		this.selectedIndex = (p + this.offset);
		post(new Runnable() {
			public void run() {
				WheelView.this.smoothScrollTo(0, p * WheelView.this.itemHeight);
			}
		});
	}

	public String getSeletedItem() {
		return (String) this.items.get(this.selectedIndex);
	}

	public int getSeletedIndex() {
		return this.selectedIndex - this.offset;
	}

	public void fling(int velocityY) {
		super.fling(velocityY / 3);
	}

	public boolean onTouchEvent(MotionEvent ev) {
		if (ev.getAction() == 1) {
			startScrollerTask();
		}
		return super.onTouchEvent(ev);
	}

	public OnWheelViewListener getOnWheelViewListener() {
		return this.onWheelViewListener;
	}

	public void setOnWheelViewListener(OnWheelViewListener onWheelViewListener) {
		this.onWheelViewListener = onWheelViewListener;
	}

	private int dip2px(float dpValue) {
		float scale = this.context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5F);
	}

	private int getViewMeasuredHeight(View view) {
		int width = View.MeasureSpec.makeMeasureSpec(0, 0);
		int expandSpec = View.MeasureSpec.makeMeasureSpec(536870911, Integer.MIN_VALUE);
		view.measure(width, expandSpec);
		return view.getMeasuredHeight();
	}

	public static class OnWheelViewListener {
		public void onSelected(int selectedIndex, String item) {
		}
	}
}
